<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Pc_Api_Standard_Line extends Controller_Pc_Api_Base
{

    public function before()
    {
        parent::before();
    }

    /*
     * 获取轮播图广告
     */
    public function action_get_rolling_ad()
    {
        $result = DB::select()->from('xcx_ads')->where('prefix','=','s_line_index')
            ->order_by('displayorder','asc')->execute()->as_array();
        foreach($result as &$v){
            //判断产品类型
            if($v['typeid'] == 1){
                $v['url'] = '/stpage/lines/pages/lineShow/lineShow';
                $mod = DB::select('litpic')->from('line');
            }
            if(!empty($mod)){
                $row = $mod ->where('id','=',$v['proid'])->execute()->current();
                //增加图片绝对访问地址
                $v['litpic'] = Model_Api_Standard_System::reorganized_resource_url($row['litpic']);
            }
        }
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);

    }

    //栏目信息
    public function action_channel()
    {
        $result = Model_Api_Standard_Line::channel();
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    /**
     * 获取线路列表
     */
    public function action_list()
    {
        //条数
        $pagesize = intval($this->request_body->content->pagesize);
        //页码
        $page = intval($this->request_body->content->page);
        //关键词
        $keyword = Common::remove_xss($this->request_body->content->keyword);
        $params = array(
            'page' => $page,
            'pagesize' => $pagesize,
            'keyword' => $keyword
        );
        $result = Model_Api_Standard_Line::search($params);

        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);

    }


    //线路详情
    public function action_detail()
    {
        $params['id'] = intval($this->request_body->content->productid);
        $params['userid'] = intval($this->request_body->content->userid);
        if($params['id'])
        {
            $result = Model_Api_Standard_Line::detail($params);

            if (empty($result))
            {
                $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key'], false, '查询线路失败', '查询线路失败');
            }
            else
            {
                $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
            }

        }


    }
	

    /**
     * 获取报价
     */
    public function action_price()
    {

        $suitid = intval($this->request_body->content->suitid);
        $row = intval($this->request_body->content->row);

        $year = intval($this->request_body->content->year);
        $month = intval($this->request_body->content->month);
        if ($suitid)
        {
            $query = DB::select()->from('line_suit_price')
                ->where('supplier_channel_id', '=', 1)
                ->and_where('suitid', '=', $suitid);
            if (empty($year) && empty($month))
            {
                $day = strtotime(date('Y-m-d'));
                $res = $query->execute()->current();
                if ($lineid = $res['lineid'])
                {
                    if (Model_Line::is_line_before($lineid))
                    {
                        $dyaBeforeNum = DB::select('linebefore')
                                          ->from('line')
                                          ->where('id', '=', $lineid)
                                          ->execute()
                                          ->get('linebefore');
                        $dayBeforeNum = !empty($dyaBeforeNum) ? $dyaBeforeNum : 0;
                        $day += $dayBeforeNum * 86400;
                    }
                }

                $query->and_where('day', '>=', $day);
            }
            else
            {
                $need_full_days = true;
                $start = strtotime("$year-$month-1");

                $end = strtotime("$year-$month-31");
                $query->and_where('day', '>=', $start);
                $query->and_where('day', '<=', $end);
                // $days = cal_days_in_month(CAL_GREGORIAN, $month, $year);
                $days = date('t', strtotime($start));

            };
            if ($row)
            {
                $query->limit($row);
            }


            $result = $query->execute()->as_array();
            $now = strtotime(date('Y-m-d'));

            if ($lineid = $result[0]['lineid'])
            {
                if (Model_Line::is_line_before($lineid))
                {
                    $dyaBeforeNum = DB::select('linebefore')
                                      ->from('line')
                                      ->where('id', '=', $lineid)
                                      ->execute()
                                      ->get('linebefore');
                    $dayBeforeNum = !empty($dyaBeforeNum) ? $dyaBeforeNum : 0;
                    $now += $dayBeforeNum * 86400;
                }
            }

            foreach ($result as &$row)
            {
                $row['digital'] = intval(date('d', $row['day']));
                //过期数据
                if ($row['day'] < $now)
                {
                    $row['adultprice'] = 0;
                    $row['childprice'] = 0;
                    $row['oldprice'] = 0;
                }
                $row['date'] = date('m-d', $row['day']);
                $row['daynum'] = intval(date('d', $row['day']));
                $row['fulldate'] = date('Y-m-d', $row['day']);
                $row['digital'] = intval(date('d', $row['day']));
                $row['adultprice'] = $row['adultprice'] == '0.00' ? Currency_Tool::price($row['childprice']) : Currency_Tool::price($row['adultprice']);

            }
            if ($need_full_days)
            {
                $insert_day = $days - count($result);
                if ($insert_day)
                {
                    for ($i = 0; $i < $insert_day; $i++)
                    {
                        $r = array(
                            'adultprice' => 0,
                            'childprice' => 0,
                            'oldprice' => 0
                        );
                        array_unshift($result, $r);
                    }
                }
            }


            if (empty($result))
            {
                $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key'], false, '获取最新报价失败', '获取最新报价失败');
            }
            else
            {

                $out = array(
                    'list' => $result
                );
                $this->send_datagrams($this->client_info['id'], $out, $this->client_info['secret_key']);
            }

        }
    }

    /**
     * @function 获取可报名人群
     */
    public function action_get_people_group()
    {
        $people_group = Model_Sysconfig::get_people_group(null,false);
        foreach($people_group as $key =>  $item) {
            if ($item['identifier'] == 2) {
                unset($people_group[$key]);
                array_unshift($people_group, $item);
                break;
            }
        }
        $this->send_datagrams($this->client_info['id'], $people_group, $this->client_info['secret_key']);
    }


    /**
     * 创建订单
     */
    public function action_create_order()
    {
        // 检查用户
        $memberid = intval($this->request_body->content->memberid);
        $productautoid = intval($this->request_body->content->productautoid);
        $suitid = intval($this->request_body->content->suitid);

        $result = array('status' => false);
        if (!$memberid || !$productautoid || !$suitid)
        {
            $result['msg'] = '提交信息有误';
            $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
        }
        Common::session('member', array('mid' => $memberid));

        $userInfo = Model_Member::get_member_info($memberid);
        if(!$userInfo)
        {
            $result['msg'] = '用户信息有误';
            $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
        }

        //产品信息
        $info = DB::select()->from('line')->where('id', '=', $productautoid)->execute()->current();
        //下架状态判断
        if($info['status']!=3)
        {
            $result['msg'] = '当前产品已下架，不可预订';
            $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
        }

        $linkman = trim(Common::remove_xss($this->request_body->content->linkman));
        $linktel = trim(Common::remove_xss($this->request_body->content->linktel));
        $linkemail = Common::remove_xss($this->request_body->content->linkemail);
        // 检查联系人和联系电话
        if(!$linkman || !$linktel)
        {
            $result['msg'] = '请填写联系人信息';
            $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
        }

        // 备注信息
        $remark = Common::remove_xss($this->request_body->content->remark);

        //积分抵现.
        $needJifen = intval($this->request_body->content->needjifen);
        $jifentprice = 0;
        $useJifen = 0;
        if ($needJifen) {
            $jifentprice = Model_Jifen_Price::calculate_jifentprice($info['jifentprice_id'], 1, $needJifen, $userInfo);
            $useJifen = empty($jifentprice) ? 0 : 1;
            $needJifen = empty($jifentprice) ? 0 : $needJifen;
        }
        //积分评论
        $jifencomment_info = Model_Jifen::get_used_jifencomment(1);
        $jifencomment = empty($jifencomment_info) ? 0 : $jifencomment_info['value'];

        // 订单编号
        $ordersn = Product::get_ordersn('01');

        // 订单数据
        $order = array(
            'ordersn'=> $ordersn,
            'litpic'=> $info['litpic'],
            'typeid'=> 1,
            'productautoid'=> $productautoid,
            'productaid'=> $info['aid'],
            'productname'=> $info['title'],
            'departdate' => $info['departdate'],
            'webid' => 0,
            'addtime' => time(),
            'linkman' => $linkman,
            'linktel' => $linktel,
            'linkemail' => $linkemail,
            'remark' => $remark,
            'usejifen' => $useJifen,
            'needjifen' => $needJifen,
            'jifentprice' => $jifentprice,
            'jifenbook' => $info['jifenbook_id'],
            'jifencomment' => $jifencomment,
            'memberid'=> $memberid,
            'suitid'=> $suitid,

        );


        //成人数量
        $dingnum = intval($this->request_body->content->dingnum);
        if($dingnum)
        {
            $order['dingnum'] = $dingnum;
        }

        //小孩数量
        $childnum = intval($this->request_body->content->childnum);
        if ($childnum)
        {
            $order['childnum'] = $childnum;
        }

        //老人数量
        $oldnum = intval($this->request_body->content->oldnum);
        if ($oldnum)
        {
            $order['oldnum'] = $oldnum;
        }

        //人群4
        $people4num = intval($this->request_body->content->people4num);
        if ($people4num)
        {
            $order['people4_num'] = $people4num;
        }
        //人群5
        $people5num = intval($this->request_body->content->people5num);
        if ($people5num)
        {
            $order['people5_num'] = $people5num;
        }
        //人群6
        $people6num = intval($this->request_body->content->people6num);
        if ($people6num)
        {
            $order['people6_num'] = $people6num;
        }

        //出发日期
        $usedate = $this->request_body->content->usedate;
        if($usedate)
        {
            $order['usedate'] = $usedate;
        }

        //单房差数量
        $roombalancenum = intval($this->request_body->content->roombalancenum);
        if ($roombalancenum)
        {
            $order['roombalancenum'] = $roombalancenum;
        }

        // 单房差默认线上支付
        $order['roombalance_paytype'] = 1;

        // 判断是否需要管理员手动确认
        $query = DB::select()->from('line_suit')->where('id', '=', $suitid)->and_where('lineid','=',$productautoid)->execute()->current();
        $need_confirm = $query['need_confirm'];
        $order['status'] = $need_confirm ? 0 : 1;

        // 套餐信息
        $suitInfo = Model_Line_Suit::suit_info($order['suitid']);

        //商品名拼接套餐名
        if($suitInfo){
            $order['productname']=$order['productname']."({$suitInfo['title']})";
        }
        // 计算定金
        $priceArr = DB::select()->from('line_suit_price')
            ->where('day', '=', strtotime($usedate))
            ->and_where('suitid', '=', $suitid)
            ->and_where('supplier_channel_id', '=', 1)
            ->execute()->current();
        if(!$priceArr)
        {
            $result['msg'] = '获取套餐信息失败';
            $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
        }

        // 保存价格相关信息
        $priceArr['adultprice'] = Currency_Tool::price($priceArr['adultprice']);
        $priceArr['childprice'] = Currency_Tool::price($priceArr['childprice']);
        $priceArr['oldprice'] = Currency_Tool::price($priceArr['oldprice']);
        $priceArr['people4_price'] = Currency_Tool::price($priceArr['people4_price']);
        $priceArr['people5_price'] = Currency_Tool::price($priceArr['people5_price']);
        $priceArr['people6_price'] = Currency_Tool::price($priceArr['people6_price']);
        $priceArr['roombalance'] = Currency_Tool::price($priceArr['roombalance']);

        $order['paytype'] = $suitInfo['paytype'];
        $order['pay_way'] = $suitInfo['pay_way'];
        $order['dingjin'] = doubleval($suitInfo['dingjin']);
        $order['price'] = doubleval($priceArr['adultprice']);
        $order['childprice'] = doubleval($priceArr['childprice']);
        $order['oldprice'] = doubleval($priceArr['oldprice']);
        $order['people4_price'] = doubleval($priceArr['people4_price']);
        $order['people5_price'] = doubleval($priceArr['people5_price']);
        $order['people6_price'] = doubleval($priceArr['people6_price']);
        $order['roombalance'] = doubleval($priceArr['roombalance']);
        $order['suit_name'] = $suitInfo['suitname'];
        $order['refund_restriction'] = $suitInfo['refund_restriction'];

        // 处理优惠券
        $couponid = $this->request_body->content->couponid;
        if ($couponid)
        {
            $cid = DB::select('cid')->from('member_coupon')->where("id={$couponid}")->execute()->current();
            $totalprice = Model_Coupon::get_order_totalprice($order);
            $ischeck = Model_Coupon::check_samount($couponid, $totalprice, $order['typeid'], $order['productautoid']);
            if ($ischeck['status'] == 1)
            {
                // 使用优惠券
                Model_Coupon::add_coupon_order($cid, $order['ordersn'], $totalprice, $ischeck, $couponid);
            }
            else
            {
                $this->send_datagrams($this->client_info['id'], array('status' => false, 'msg' => '优惠券不满足使用条件', 'ext' => array($couponid, $totalprice, $order['typeid'], $order['productautoid'])), $this->client_info['secret_key']);
            }
        }

        // 订单发票信息
        $receipt = $this->request_body->content->receiptInfo;
        $bill_info = '';
        $usebill = false;
        if($receipt)
        {
            $receiptInfo = json_decode($receipt,true);
            if($receiptInfo)
            {
                //发票
                $invoice_type = $receiptInfo['type'];
                $bill_info = array(
                    'title' => $receiptInfo['title'],
                    'content' => $receiptInfo['content'],
                    'type' => $invoice_type,
                    'taxpayer_number' => $invoice_type!=0  ? $receiptInfo['taxpayer_number']:'',
                    'taxpayer_address' => $invoice_type==2 ? $receiptInfo['taxpayer_address']:'',
                    'taxpayer_phone' => $invoice_type==2   ? $receiptInfo['taxpayer_phone']:'',
                    'bank_branch' => $invoice_type==2      ? $receiptInfo['bank_branch']:'',
                    'bank_account' => $invoice_type==2     ? $receiptInfo['bank_account']:''
                );

                // 发票地址
                if(isset($receiptInfo['address']) && !empty($receiptInfo['address']))
                {
                    $receiptAddress = $receiptInfo['address'];
                    $bill_info['mobile'] = $receiptAddress['phone'];
                    $bill_info['receiver'] = $receiptAddress['receiver'];
                    $bill_info['postcode'] = $receiptAddress['postcode'];
                    $bill_info['province'] = $receiptAddress['province'];
                    $bill_info['city'] = $receiptAddress['city'];
                    $bill_info['address'] = $receiptAddress['address'];
                }
                $usebill = true;
            }
        }

        // 自动关闭订单时间
        $auto_close_time = $suitInfo['auto_close_time'] ? $suitInfo['auto_close_time'] : 0;
        if ($auto_close_time)
        {
            $order['auto_close_time'] = $auto_close_time + time();
        }

        // 订单来源 1PC,2WAP,3小程序,4平台添加
        $order['source'] = 3;

		// 游客信息
        $tourist = $this->request_body->content->tourist;

        // 保险信息
        $planid = $this->request_body->content->planid;
        $baseid = $this->request_body->content->baseid;

        // 拼团
        $join_id = empty($this->request_body->content->teamid) ? 0 : $this->request_body->content->teamid;
        if ($order['memberid'])
        {
            // 保存订单
            $out = Model_Api_Standard_Order::add_order($order, 'Model_Line');
            if ($out && $out['status']!=0)
            {
                $order_id = DB::select('id')->from('member_order')->where('ordersn', '=', $order['ordersn'])->execute()->get('id');
                $order_info = Model_Member_Order::order_info($order['ordersn']);
                $order_info['orderid'] = $order_id;
                $order_info['need_confirm'] = $need_confirm;

				//游客信息保存
				$arr = array();
				foreach($tourist as $k => $v)
                {
					$arr['orderid']    = $order_id;
					$arr['tourername'] = $v->linkman;
					$arr['sex'] = $v->sex;
					$arr['mobile'] = $v->mobile;
					$arr['cardtype'] = $v->cardtype;
					$arr['cardnumber'] = $v->idcard;
					$arr['en_first_name'] = $v->en_first_name;
					$arr['en_last_name'] = $v->en_last_name;
					$arr['nationality'] = $v->nationality;
					$arr['cardnumber_expire_date'] = $v->cardnumber_expire_date;
					$arr['birth'] = $v->birth;
					DB::insert('member_order_tourer', array_keys($arr))->values(array_values($arr))->execute();
				}

                // 保险信息
                if(!empty($planid))
                {
                    $params = array(
                        'orderSn' => $order['ordersn'],
                        'dingnum' => $order['dingnum'] + $order['childnum'] + $order['oldnum'],
                        'baseid' => $baseid,
                        'planid' => $planid,
                        'tours' => $arr,
                        'memberid' => $order['memberid'],
                        'usedate' => $order['usedate']
                    );
                    Model_Insurance_Base::add_product_order($params);
                }

                // 添加发票
                if ($usebill)
                {
                    Model_Member_Order_Bill::add_bill_info($order_id, $bill_info);
                }

                //添加拼团
                if(St_Functions::is_normal_app_install('together'))
                {
                    Model_Together::join_together($order['ordersn'],1,$order['productautoid'],$join_id);
                }

                //订单结算信息表数据更新
                Model_Member_Order_Compute::update($order['ordersn']);

                $result = array(
                    'status' => true,
                    'orderinfo' => $order_info
                );

                $this->send_datagrams($this->client_info['id'],$result,$this->client_info['secret_key']);
            }
            else
            {
                $result = array(
                    'status' => false,
                    'msg' => $out['msg']
                );
                $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
            }
        }
    }



    //关键词搜索
    public function action_query_by_keyword()
    {
        $keyword = $this->request_body->content->keyword;
        $p = intval($this->request_body->content->p) or $p = 1;
        $pagesize = intval($this->request_body->content->pagesize) or $pagesize = 10;
        $route_array = array(
            'pinyin' => 'line',
            'keyword' => $keyword,
            'typeid' => 1
        );
        $searchcode = St_String::split_keyword($keyword);
        $result = Model_Global_Search::search_result($route_array, $searchcode, $p, $pagesize);
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    //首页推荐
    public function action_index_recommend()
    {
        $p = intval($this->request_body->content->p) or $p = 1;
        $pagesize = intval($this->request_body->content->pagesize) or $pagesize = 4;
        $result = Taglib_Line::query(array('flag' => 'order', 'row' => $pagesize));
        foreach ($result as &$item)
        {
            $item['litpic'] = Common::img($item['litpic']);
        }
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    //查询条件
    public function action_query_condition()
    {
        $result = array(
            'sort' => array(
                array('id' => 0, 'name' => '综合排序'),
                array('id' => 1, 'name' => '价格从低到高'),
                array('id' => 2, 'name' => '价格从高到低'),
                array('id' => 3, 'name' => '销量最高'),
                array('id' => 4, 'name' => '产品推荐')
            ),

            'start_city' => array('name' => '出发地', 'child' => array()),
            'attribute' => array(),
            'day' => array('name' => '天数', 'child' => array()),
            'price' => array('name' => '价格', 'child' => array()),

        );
        //出发地
        $result['start_city']['child'] = DB::select('id', array('cityname', 'name'))->from('startplace')->where('isopen', '=', 1)->and_where('pid', '>', 0)->order_by('displayorder', 'asc')->execute()->as_array();
        //属性
        $result['attribute'] = DB::select('id', array('attrname', 'name'))->from('line_attr')->where('pid', '=', 0)->and_where('isopen', '=', 1)->order_by(DB::expr('ifnull(displayorder,9999)'), 'asc')->execute()->as_array();
        foreach ($result['attribute'] as &$item)
        {
            $item['child'] = DB::select('id', array('attrname', 'name'))->from('line_attr')->where('pid', '=', $item['id'])->and_where('isopen', '=', 1)->order_by(DB::expr('ifnull(displayorder,9999)'), 'asc')->execute()->as_array();

        }
        //天数
        $result['day']['child'] = DB::select()->from('line_day')->execute()->as_array();
        $autoindex = 1;
        foreach ($result['day']['child'] as &$r)
        {
            $number = substr($r['word'], 0, 2);
            $arr = array("零", "一", "二", "三", "四", "五", "六", "七", "八", "九");
            if (strlen($number) == 1)
            {
                $days = $arr[$number];
            }
            else
            {
                if ($number == 10)
                {
                    $days = "十";
                }
                else
                {
                    if ($number < 20)
                    {
                        $days = "十";
                    }
                    else
                    {
                        $days = $arr[substr($number, 0, 1)] . "十";
                    }
                    if (substr($number, 1, 1) != "0")
                    {
                        $days .= $arr[substr($number, 1, 1)];
                    }
                }
            }
            if ($autoindex == count($result['filter']['day']['child']))
            {
                $r['name'] = $days . "日游以上";
            }
            else
            {
                $r['name'] = $days . "日游";
            }

            $autoindex++;
        }
        $result['price']['child'] = DB::select()->from('line_pricelist')->where('lowerprice', '>=', 0)->order_by('lowerprice', 'asc')->execute()->as_array();

        //价格
        foreach ($result['price']['child'] as &$row)
        {
            if ($row['lowerprice'] == 0 && $row['highprice'] != 0)
            {
                $row['name'] = Currency_Tool::symbol() . $row['highprice'] . '以下';
            }
            else if ($row['highprice'] == '' || $row['highprice'] == 0)
            {
                $row['name'] = Currency_Tool::symbol() . $row['lowerprice'] . '以上';
            }
            else if ($row['lowerprice'] != '' && $row['highprice'] != '')
            {
                $row['name'] = Currency_Tool::symbol() . $row['lowerprice'] . '-' . Currency_Tool::symbol() . $row['highprice'];
            }
        }
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    public function action_general_query()
    {
        $params = array(
            'pagesize' => $this->request_body->content->pagesize,
            'keyword' => $this->request_body->content->keyword,
        );
        $path = $this->request_body->content->path;
        list($params['destpy'], $params['dayid'], $params['priceid'], $params['sorttype'], , $params['startcityid'], $params['attrid'], $params['page']) = explode('-', $path);

        $result = Model_Api_Standard_Line::search($params);
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }
	// 获取游客信息是否开启
	public function action_get_tourists()
	{
		$result = DB::select()->from('sysconfig')->where('varname', '=', 'cfg_write_tourer')->execute()->current();
		$this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
	}

	/**
     * 线路热门目的地
     */
	public function action_get_index_hot_dest()
    {
        $arr = Model_Destinations::get_hot_dest(1, 0, 6);
        foreach($arr as &$item){
            $item['litpic'] = Common::img($item['litpic']);
        }
        $this->send_datagrams($this->client_info['id'], $arr, $this->client_info['secret_key']);
    }

    /**
     * 线路首页目的地
     */
    public function action_get_index_dest()
    {
        $arr = Model_Destinations::get_channel_nav(0, 10, 1);
        $this->send_datagrams($this->client_info['id'], $arr, $this->client_info['secret_key']);
    }

    /**
     * 线路首页属性
     */
    public function action_get_index_attr()
    {
        $arr = Model_Api_Standard_System::get_index_attr(1);
        $this->send_datagrams($this->client_info['id'], $arr, $this->client_info['secret_key']);
    }

    /**
     * 根据目的地获取线路
     */
    public function action_get_dest_list()
    {
        $destid = $this->request_body->content->destid;
        $offset = $this->request_body->content->page;
        $row = $this->request_body->content->pagesize;
        $offset = empty($offset) ? 0 : $offset*$row;
        $arr = Taglib_Line::query(array('flag' => 'mdd','destid' => $destid,'offset' => $offset,'row' => $row));
        foreach($arr as &$item){
            $item['litpic'] = Common::img($item['litpic']);
        }
        $this->send_datagrams($this->client_info['id'], $arr, $this->client_info['secret_key']);
    }

    /**
     * 获取线路套餐
     */
    public function action_get_suit_list()
    {
        $id = $this->request_body->content->id;
        $result = Model_Api_Standard_Line::suit($id);
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);

    }

    //获取报价日历html新版
    public function action_ajax_price_calendar_json()
    {
        $suitid = $this->request_body->content->suitid;
        $productid = $this->request_body->content->productid;
        $startdate = $this->request_body->content->date;
        $year = $this->request_body->content->year;
        $month = $this->request_body->content->month;

        $info = DB::select('islinebefore', 'linebefore')->from('line')->where('id', '=', $productid)->execute()->current();
        $ext = array();
        $ext['islinebefore'] = $info['islinebefore'];
        $ext['linebefore'] = $info['linebefore'];
        $out = '';
        if(!$startdate)
        {
            $startday = date('Y-m-d');
            //线路提前预订
            $day = strtotime($startday);

            if ($ext['islinebefore'] > 0 && $ext['linebefore'] > 0)
            {
                $day = strtotime("+{$ext['linebefore']} days", $day);
            }
            if(!$month||!$year)
            {
                $year = date('Y',$day);
                $month = date('m',$day);
            }

            $startdate =   date('Y-m-d', $day);
        }
        else
        {
            $day = strtotime($startdate);
            $year = date('Y',$day);
            $month = date('m',$day);
        }

        $price_arr = Model_Line::get_month_price($year, $month, $suitid, $startdate);
        $cfg_line_minprice_rule = $GLOBALS['cfg_line_minprice_rule'];
        $data=array();
        foreach ($price_arr as $k=>&$v)
        {
            $price = 0;

            switch ($cfg_line_minprice_rule)
            {
                case 1:
                    $price = $v['child_price'];
                    break;
                case 2:
                    $price = $v['price'];
                    break;
                case 3:
                    $price = $v['old_price'];
                    break;
                default:
                    $price = $v['people'.$cfg_line_minprice_rule.'_price'];
                    break;
            }


            $price =empty($price)? min(array_filter(array(floatval($v['child_price']),floatval($v['price']),floatval($v['old_price']),floatval($v['people4_price']),floatval($v['people5_price']),floatval($v['people6_price'])))):$price;

            $before=!($ext['islinebefore'] > 0 && $ext['linebefore'] > 0) ? strtotime(date('Ymd')." 0:0:0"):strtotime("+{$ext['linebefore']} days", strtotime(date('Ymd')." 0:0:0"));
            if($k < $before){
                continue;
            }
            $data[date('Ymd',$k)]=array(
                'date' => date('Ymd',$k),
                'times' => date('Y-m-d',$k),
                'day' => intval(date('d',$k)),
                'price'=>$price,
                'child_price'=>$v['child_price'],
                'old_price'=>$v['old_price'],
                'number'=>$v['number'],
                'adult_price'=>$v['price'],
                'people4_price'=>$v['people4_price'],
                'people5_price'=>$v['people5_price'],
                'people6_price'=>$v['people6_price'],
                'roombalance'=>$v['roombalance']
            );
        }
        $this->send_datagrams($this->client_info['id'], $data, $this->client_info['secret_key']);
    }

    /**
     * 查询线路title 等信息
     */
    public function action_line_title()
    {
        $productid = $this->request_body->content->productid;
        $info = DB::select('id','title','linebefore','lineday','jifentprice_id')->from('line')->where('id','=',$productid)->execute()->current();
        $info['series'] = St_Product::product_series($info['id'], 1);
        $this->send_datagrams($this->client_info['id'], $info, $this->client_info['secret_key']);
    }

    /**
     * 获取拼团列表
     */
    public function action_getTogether()
    {
        $productid = $this->request_body->content->productid;
        $page = empty($this->request_body->content->page) ? 2 : $this->request_body->content->page;
        $info['together'] = Model_Together::get_product_show_list(1,$productid);
        $planlist = Model_Together::get_add_product_list(1,$productid);
        $together_list = Model_Together::together_join_list(1,$productid,$page,$planlist);
        foreach ($together_list as &$t)
        {
            $t['member']['litpic'] = Model_Api_Standard_System::reorganized_resource_url($t['member']['litpic']);
        }
        $info['together_list'] = $together_list;
        $this->send_datagrams($this->client_info['id'], $info, $this->client_info['secret_key']);
    }

    /**
     * 平团详情
     */
    public function action_getTogetherShow()
    {
        $productid = $this->request_body->content->productid;
        $data['info'] = Model_Together::show_info($productid);
        $together_list= Model_Together::get_join_child_list($productid);
        $data['info']['litpic'] = Model_Api_Standard_System::reorganized_resource_url($data['info']['litpic']);
        $data['info']['series'] =  $info['series'] = St_Product::product_series($data['info']['productid'], 1);;
        foreach ($together_list as &$t)
        {
            $t['member']['litpic'] = Model_Api_Standard_System::reorganized_resource_url($t['member']['litpic']);
        }
        $data['child_list'] = $together_list;
        $suitlist = Model_Together::get_product_suit($data['info']);
        $data['suitlist'] = array_values($suitlist);

        $this->send_datagrams($this->client_info['id'], $data, $this->client_info['secret_key']);
    }
}
